﻿using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenXmlTools.OpenXML.Word
{
    /// <summary>
    /// Word表格文字字体帮助类
    /// </summary>
    public static class WordTableFontHelper
    {
        /// <summary>
        /// 设置Word表格文字字体
        /// </summary>
        /// <param name="table">要设置字体的表格对象</param>
        /// <param name="fontSettings">字体设置配置</param>
        /// <param name="applyTo">应用范围（整个表格、特定行或特定单元格）</param>
        /// <param name="rowIndex">当applyTo为Row时的行索引</param>
        /// <param name="cellIndex">当applyTo为Cell时的单元格索引</param>
        public static void SetTableFont(Table table, FontSettings? fontSettings = null, TableApplyTarget applyTo = TableApplyTarget.EntireTable, int rowIndex = 0, int cellIndex = 0)
        {
            fontSettings ??= new FontSettings();
            switch (applyTo)
            {
                case TableApplyTarget.EntireTable:
                    ApplyToEntireTable(table, fontSettings);
                    break;
                case TableApplyTarget.Row:
                    ApplyToSpecificRow(table, fontSettings, rowIndex);
                    break;
                case TableApplyTarget.Cell:
                    ApplyToSpecificCell(table, fontSettings, rowIndex, cellIndex);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 应用到整个表格
        /// </summary>
        /// <param name="table">要设置字体的表格对象</param>
        /// <param name="settings">字体设置配置</param>
        private static void ApplyToEntireTable(Table table, FontSettings settings)
        {
            foreach (TableRow row in table.Elements<TableRow>())
            {
                foreach (TableCell cell in row.Elements<TableCell>())
                {
                    ApplyToTableCell(cell, settings);
                }
            }
        }

        /// <summary>
        /// 应用到特定行
        /// </summary>
        /// <param name="table">要设置字体的表格对象</param>
        /// <param name="settings">字体设置配置</param>
        /// <param name="rowIndex">行索引</param>
        private static void ApplyToSpecificRow(Table table, FontSettings settings, int rowIndex)
        {
            var rows = table.Elements<TableRow>().ToList();
            if (rowIndex >= 0 && rowIndex < rows.Count)
            {
                foreach (TableCell cell in rows[rowIndex].Elements<TableCell>())
                {
                    ApplyToTableCell(cell, settings);
                }
            }
        }

        /// <summary>
        /// 应用到特定单元格
        /// </summary>
        /// <param name="table">要设置字体的表格对象</param>
        /// <param name="settings">字体设置配置</param>
        /// <param name="rowIndex">行索引</param>
        /// <param name="cellIndex">列索引</param>
        private static void ApplyToSpecificCell(Table table, FontSettings settings, int rowIndex, int cellIndex)
        {
            var rows = table.Elements<TableRow>().ToList();
            if (rowIndex >= 0 && rowIndex < rows.Count)
            {
                var cells = rows[rowIndex].Elements<TableCell>().ToList();
                if (cellIndex >= 0 && cellIndex < cells.Count)
                {
                    ApplyToTableCell(cells[cellIndex], settings);
                }
            }
        }

        /// <summary>
        /// 应用到单个表格单元格
        /// </summary>
        /// <param name="cell">单元格</param>
        /// <param name="settings">字体设置配置</param>
        private static void ApplyToTableCell(TableCell cell, FontSettings settings)
        {
            foreach (Paragraph paragraph in cell.Elements<Paragraph>())
            {
                foreach (Run run in paragraph.Elements<Run>())
                {
                    // 获取或创建运行属性
                    RunProperties? rPr = run.RunProperties;
                    if (rPr == null)
                    {
                        rPr = new RunProperties();
                        run.RunProperties = rPr;
                    }

                    // 应用字体设置
                    settings.ApplyTo(rPr);
                }
            }
        }

    }
}
